package com.fruit.controller.wap.rights;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.goods.Base;
import com.fruit.pojo.order.MailOrder;
import com.fruit.pojo.rights.UserGoodsRightReslut;
import com.fruit.pojo.rights.UserGoodsRights;
import com.fruit.pojo.rights.UserGoodsRightsHarvest;
import com.fruit.pojo.rights.UserGoodsRightsPost;
import com.fruit.service.goods.GoodsManager;
import com.fruit.service.order.MailOrderManager;
import com.fruit.service.rights.UserRightsManager;
import com.fruit.util.CommonUtil;
import com.fruit.util.DateUtil;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author 葛欢欢
 * @version 2017.11.30 12:20
 * @ClassName: UserGoodsRightControllerWap
 */
@Slf4j
@Controller
@RequestMapping(value = "/wap/rights")
public class UserGoodsRightControllerWap extends BaseController {

	@Autowired
	private UserRightsManager userRightsManager;

	@Autowired
	private GoodsManager goodsManager;

	@Autowired
	private MailOrderManager mailOrderService;

	/**
	 * 获取用户权益信息详情
	 *
	 * @param id
	 *            权益信息ID
	 * @return Map<String, Object>
	 * @author 葛欢欢
	 * @version 2017.11.30 12:20
	 */
	@RequestMapping(value = "getUserGoodsRight", method = { RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> getUserGoodsRight(HttpServletRequest request, @RequestParam("id") int id,
			@SessionAttribute("userId") int userId) {
		long a = System.currentTimeMillis();
		log.info("【获取用户权益信息详情进入时间】" + DateUtil.getTime());
		log.info("【获取用户权益信息详情】---【开始】---");
		Map<String, Object> resMap = new HashMap<>();
		log.info("【获取用户权益信息ID】 " + id);
		if (id <= 0) {
			resMap.put("result", this.initResult(ResultInfo.ERROR, "ERROR", "参数错误"));
		} else {
			UserGoodsRights right = userRightsManager.getUserGoodsRight(id);
			Base base = new Base();
			if (right.getBaseId() != 0) {
				base = goodsManager.getBase(right.getBaseId());
			}
			List<UserGoodsRightsHarvest> ugrsList = null;
			List<UserGoodsRightsPost> bulletinList = null;
			Map<String, Object> params = new HashMap<String, Object>();
			List<MailOrder> getMailOrder = null;
			List<UserGoodsRightReslut> getReslut = new ArrayList<UserGoodsRightReslut>();
			Map<String, Object> result = new HashMap<String, Object>();
			if (right.getId() != 0) {
				String buyTime = right.getBuyTime();// 没有进行格式化的 时间
				ugrsList = userRightsManager.findUserGoodsRightsHarvest((int) right.getId());
				if (ugrsList != null && ugrsList.size() != 0) {
					for (int i = 0; i < ugrsList.size(); i++) {
						UserGoodsRightsHarvest urh = ugrsList.get(i);
						if (DateUtil.compareDate(DateUtil.getDay(), urh.getExpectHarvestTime())) { // 如果预计收获时间大于当前时间，那么说明还没有收获，否则已经收获
							urh.setState("00"); // 未收获
						} else {
							urh.setState("01"); // 已收获
						}
						if (DateUtil.compareDate(DateUtil.getDay(), DateUtil.yMdhms(right.getEndUseTime()))) {// 如果当前时间大于等于截止时间
																												// 那么最后的结束要点亮
							urh.setEndState("00");// 已经截止啦
						} else {
							urh.setEndState("01");// 未截止
						}
						String expectHarvestTime = DateUtil.ChTime(urh.getExpectHarvestTime());
						urh.setExpectHarvestTime(expectHarvestTime.substring(0, 8));
					}
				}
				// 类型 00 权益消息气泡 01 首页消息气泡 02 钱包首页轮询消息
				bulletinList = userRightsManager.findUserGoodsRightsPost("00");
				right.setBirthday(DateUtil.ChTime(right.getBirthday()).substring(0, 8));
				if (right.getBuyTime() != null) {
					right.setBuyTime(DateUtil.ChTime(right.getBuyTime()));
				}
				// 收果信息
				String tradeId = CommonUtil.getUUID();
				if (userId <= 0) {
					log.info("[{}]-【权益详情】-【用户：{}】-【开始】", tradeId, userId);
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "user_not_exists", "用户不存在!"));
				} else {
					if (right.getOrderGoodsId() <= 0) {
						log.info("[{}]-【权益详情】-【订单：{}】-【开始】", tradeId, userId);
						resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "order_not_exists", "订单不存在!"));
					} else {
						params.put("orderGoodsId", right.getOrderGoodsId());
						params.put("userId", userId);

						getMailOrder = mailOrderService.getMailOrder(tradeId, params);
						if (getMailOrder != null) {
							for (int i = 0; i < getMailOrder.size(); i++) {
								List<String> resultList = new ArrayList<String>();
								UserGoodsRightReslut rightReulst = new UserGoodsRightReslut();
								MailOrder mailOrder = getMailOrder.get(i);
								rightReulst.setMailOrderId(mailOrder.getId());
								rightReulst.setBatch(mailOrder.getBatch() + "");// 批次
								int num = i + 1;
								if (i == 0) {// 第一批
									// 如果当前时间大于购买时间 说明果树正在成长中
									if (DateUtil.compareDate2(DateUtil.getTime(), buyTime)) {
										resultList.add("恭喜，园主认养成功:" + DateUtil.yMdhms(right.getBuyTime()));
									} else {
										break;
									}
								}
								if (DateUtil.compareDate2(DateUtil.getTime(), buyTime)) {
									if (getMailOrder.size() == 1) {
										resultList.add("正在茁壮成长");
									} else {
										resultList.add("正在茁壮成长(第" + num + "批)");
									}
								} else {
									break;
								}
								if (StringUtils.isBlank(mailOrder.getRealHarvestTime())) {
									rightReulst.setDescription(resultList);
									getReslut.add(rightReulst);
									break;
								} else {
									if (DateUtil.compareDate(DateUtil.getDay(), mailOrder.getRealHarvestTime())) {
										if (getMailOrder.size() == 1) {
											resultList.add("园主，已为您采摘:" + mailOrder.getRealHarvestTime());
										} else {
											resultList.add("园主，第" + num + "批已为您采摘:" + mailOrder.getRealHarvestTime());
										}

									} else {
										rightReulst.setDescription(resultList);
										getReslut.add(rightReulst);
										break;
									}
								}
								if (StringUtils.isBlank(mailOrder.getDeliveryTime())) {
									rightReulst.setDescription(resultList);
									getReslut.add(rightReulst);
									break;
								} else {
									if (DateUtil.compareDate(DateUtil.getDay(), mailOrder.getDeliveryTime())) {
										if ("02".equals(mailOrder.getState())) {
											resultList.add("已发货，请注意查收:" + mailOrder.getDeliveryTime());
										} else if ("03".equals(mailOrder.getState())) {
											resultList.add("已收货，请注意查收:" + mailOrder.getClosingTime());
										} else {
											rightReulst.setDescription(resultList);
											getReslut.add(rightReulst);
											break;
										}
										// 00 查看物流按钮隐藏 01 查看物流按钮显示
										rightReulst.setIsDisplay("01");
									}
									rightReulst.setDescription(resultList);
									getReslut.add(rightReulst);
								}
								if (i == getMailOrder.size() - 1) {
									if (DateUtil.compareDate(DateUtil.getDay(),
											DateUtil.yMdhms(right.getEndUseTime()))) { 
										resultList.add("认养到期:" + DateUtil.yMdhms(right.getEndUseTime()));
									}
								}
							}
							result.put("getReslut", getReslut);
						}
					}
				}
			}
			resMap.put("result", this.initResult(ResultInfo.SUCCESS, "SUCCESS", "查询成功!"));
			resMap.put("rights", right);
			resMap.put("base", base);
			resMap.put("ugrsList", ugrsList);
			resMap.put("bulletinList", bulletinList);
			resMap.put("result", result);
			log.info("【用户权益信息详情返回值right：】" + right + "base:" + base + "ugrsList:" + ugrsList + "bulletinList:"
					+ bulletinList + "result" + result);
		}
		log.info("【获取用户权益信息详情】---【结束】---");
		log.info("【获取用户权益信息详情耗时】" + (System.currentTimeMillis() - a) / 1000f + "秒");
		return resMap;
	}

	/**
	 * 收果信息 组合
	 * 
	 * @author 葛欢欢
	 * @version 2017.11.30 16:40
	 * @return
	 */
	public Map<String, Object> getRestult(Map<String, Object> params, String tradeId) {
		log.info("[{}]-【权益详情】-【收果信息：{}】-【开始】", params);
		Map<String, Object> resMap = new HashMap<>();
		List<MailOrder> getMailOrder = mailOrderService.getMailOrder(tradeId, params);
		if (getMailOrder != null) {
			for (int i = 0; i < getMailOrder.size(); i++) {

			}
		}
		return resMap;
	}

	/**
	 * 获取消息
	 * 
	 * @param request
	 *            request
	 * @param session
	 *            session
	 * @return Map<String, Object>
	 * @author 葛欢欢
	 * @version 2017.11.30 12:20
	 */
	@RequestMapping(value = "getNews", method = { RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> getNews(HttpServletRequest request, HttpSession session) {
		long a = System.currentTimeMillis();
		log.info("【获取消息进入时间】" + DateUtil.getTime());
		log.info("【获取消息】---【开始】---");
		Map<String, Object> resMap = new HashMap<>();
		// 类型 00 权益消息气泡 01 首页消息气泡 02 钱包首页轮询消息
		List<UserGoodsRightsPost> bulletinList = userRightsManager.findUserGoodsRightsPost("02");
		resMap.put("bulletinList", bulletinList);
		log.info("【获取消息返回值bulletinList：】 " + bulletinList);
		log.info("【获取消息】---【结束】---");
		log.info("【获取消息耗时】" + (System.currentTimeMillis() - a) / 1000f + "秒");
		return resMap;
	}

	// public static void main(String[] args) {
	// if (DateUtil.compareDate("2018-01-08", DateUtil.getDay())) { //
	// 如果预计收获时间大于当前时间，那么说明还没有收获，否则已经收获
	// System.out.println("01");
	// System.out.println(DateUtil.ChTime("2018-01-08"));
	// } else {
	// System.out.println("00");
	// System.out.println(DateUtil.ChTime("2018-01-08"));
	// }
	// String str = "2018年10月12号";
	// System.out.println(str.substring(0,8));
	// }
}
